using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Globalization;
using System.Reflection;
using Microsoft.Win32;


namespace AKS_Size_Utility
{
    public partial class Form1 : Form
    {
        float pxems;
        float pxper;
        float ptsems;
        float ptsper;
        
        RegistryKey key;

        string skey = "";
                
        public Form1()
        {        
            InitializeComponent();

            // Attempt to open the key
            Registry.LocalMachine.OpenSubKey("Software\\HISC\\Size\\BrowserMsg");

            // If the return value is null, the key doesn't exist
            if (key == null)
            {
                // The key doesn't exist; create it / open it
                key = Registry.LocalMachine.CreateSubKey("Software\\HISC\\Size");
                key.SetValue("BrowserMsg","0");
                showBrowserMessageToolStripMenuItem.Checked = true;
            }

            // Attempt to retrieve the value X; if null is returned, the value
            // doesn't exist in the registry.
            if (key.GetValue("BrowserMsg") != null)
            {
                skey = key.GetValue("BrowserMsg").ToString();
            }
            else
            {
                key.SetValue("BrowserMsg","0");
                showBrowserMessageToolStripMenuItem.Checked = true;

            }

            /*if (key.GetValue("BrowserMsg").ToString() == "0")
            {
                showBrowserMessageToolStripMenuItem.Checked = true;
            }*/
                        
            ResourceManager rm = new ResourceManager("AKS_Size_Utility.Resource1", Assembly.GetExecutingAssembly());

            BtnCalculate.Text = rm.GetString("BtnCalculatetext");
            btnClose.Text = rm.GetString("btnClosetext");
            btnCopyResult.Text = rm.GetString("btnCopyResulttext");
            btnResetDefaults.Text = rm.GetString("btnResetDefaultstext");
            btnSetFormula.Text = rm.GetString("btnSetFormulatext");
            gbxResult.Text = (rm.GetString("gbxResulttext"));
            groupBox1.Text = rm.GetString("groupBox1text");
            label3.Text = rm.GetString("label3text");
            label5.Text = rm.GetString("label5text");
            lblConverFrom.Text = rm.GetString("lblConverFromtext");
            lblConvertTo.Text = rm.GetString("lblConvertTotext");
            lblOutputToAFile.Text = rm.GetString("lblOutputToAFile");
            lblFrom.Text = rm.GetString("lblFrom");
            lblTo.Text = rm.GetString("lblTo");
            lblIteration.Text = rm.GetString("lblIteration");
            btnGenerate.Text = rm.GetString("btnGenerate");
            fileToolStripMenuItem.Text = rm.GetString("MenuFile");
            closeToolStripMenuItem.Text = rm.GetString("MenuFileClose");
            settingsToolStripMenuItem.Text = rm.GetString("MenuSettings");
            showBrowserMessageToolStripMenuItem.Text = rm.GetString("MenuSettingsShowBrowserMessage");
            helpToolStripMenuItem.Text = rm.GetString("MenuHelp");
            aboutToolStripMenuItem.Text = rm.GetString("MenuHelpAbout");

            if (!File.Exists("formulas.ini"))
            {
                TextWriter tw = new StreamWriter("formulas.ini");
                tw.WriteLine("0.0625");
                tw.WriteLine("6.25");
                tw.WriteLine("0.083");
                tw.WriteLine("8.333");
                tw.Close();

                pxems = float.Parse("0.0625");
                pxper = float.Parse("6.25");
                ptsems = float.Parse("0.083");
                ptsper = float.Parse("8.333");

                tw.Close();
            }
            else
            {
                TextReader tr = new StreamReader("formulas.ini");

                try
                {
                    pxems = float.Parse(tr.ReadLine());                    
                }
                catch
                {
                    pxems = float.Parse("0.0625");
                }
                try
                {
                    pxper = float.Parse(tr.ReadLine());
                }
                catch
                {
                    pxper = float.Parse("6.25");
                }
                try
                {
                    ptsems = float.Parse(tr.ReadLine());
                }
                catch
                {
                    ptsems = float.Parse("0.083");
                }
                try
                {
                    ptsper = float.Parse(tr.ReadLine());
                }
                catch
                {
                    ptsper = float.Parse("8.333");
                }

                tr.Close();

                TextWriter tw = new StreamWriter("formulas.ini");
                tw.WriteLine("0.0625");
                tw.WriteLine("6.25");
                tw.WriteLine("0.083");
                tw.WriteLine("8.333");
                tw.Close();
            }

            txtbxValue.KeyDown += new KeyEventHandler(txtbxValue_KeyDown);
            txtbxValue.Leave += new EventHandler(txtbxValue_Leave);
        }

        void txtbxValue_Leave(object sender, EventArgs e)
        {
            BtnCalculate_Click_1(sender, e);
        }

        void txtbxValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BtnCalculate_Click_1(sender, e);
            }
        }

        private void cbFrom_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DisplayFormula();
        }

        private void cbTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayFormula();
        }

        private void DisplayFormula()
        {           
            if (cbFrom.Text == "Pixels")
            {
                if (cbTo.Text == "Ems")
                {
                    txtbxFormula.Text = pxems.ToString();
                }
                else if (cbTo.Text == "Percent")
                {
                    txtbxFormula.Text = pxper.ToString();
                }
            }
            else if (cbFrom.Text == "Points")
            {
                if (cbTo.Text == "Ems")
                {
                    txtbxFormula.Text = ptsems.ToString();
                }
                else if (cbTo.Text == "Percent")
                {
                    txtbxFormula.Text = ptsper.ToString();
                }
            }
        }

        private void btnSetFormula_Click(object sender, EventArgs e)
        {
            TextWriter tw = new StreamWriter("formulas.ini");

            if (cbTo.Text != "" || cbFrom.Text != "")
            {
                if (cbFrom.Text == "Pixels")
                {
                    if (cbTo.Text == "Ems")
                    {
                        pxems = float.Parse(txtbxFormula.Text);

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                    else if (cbTo.Text == "Percent")
                    {
                        pxper = float.Parse(txtbxFormula.Text);

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                }
                else if (cbFrom.Text == "Points")
                {
                    if (cbTo.Text == "Ems")
                    {
                        ptsems = float.Parse(txtbxFormula.Text);

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                    else if (cbTo.Text == "Percent")
                    {
                        ptsper = float.Parse(txtbxFormula.Text);

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                }
            }
            tw.Close();
        }

        private void btnResetDefaults_Click(object sender, EventArgs e)
        {
            TextWriter tw = new StreamWriter("formulas.ini");

            if (cbTo.Text != "" || cbFrom.Text != "")
            {
                if (cbFrom.Text == "Pixels")
                {
                    if (cbTo.Text == "Ems")
                    {
                        pxems = float.Parse(".0625");
                        txtbxFormula.Text = "0.0625";

                        tw.WriteLine("0.0625");
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                    else if (cbTo.Text == "Percent")
                    {
                        pxper = float.Parse("6.25");
                        txtbxFormula.Text = "6.25";

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine("6.25");
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine(ptsper.ToString());
                    }
                }
                else if (cbFrom.Text == "Points")
                {
                    if (cbTo.Text == "Ems")
                    {
                        ptsems = float.Parse(".083");
                        txtbxFormula.Text = "0.083";

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine("0.083");
                        tw.WriteLine(ptsper.ToString());
                    }
                    else if (cbTo.Text == "Percent")
                    {
                        ptsper = float.Parse("8.333");
                        txtbxFormula.Text = "8.333";

                        tw.WriteLine(pxems.ToString());
                        tw.WriteLine(pxper.ToString());
                        tw.WriteLine(ptsems.ToString());
                        tw.WriteLine("8.333");
                    }
                }
            }
            tw.Close();
        }

        private void btnCopyResult_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(rtbResult.Text);
        }

        private void BtnCalculate_Click_1(object sender, EventArgs e)
        {
            string txtbxval = txtbxValue.Text;

            if (cbTo.Text != "" && cbFrom.Text != "")
            {
                try
                {
                    float val = float.Parse(txtbxval);

                    if (cbFrom.Text == "Pixels")
                    {
                        if (cbTo.Text == "Ems")
                        {
                            val = (val * pxems);
                            rtbResult.Text = val.ToString();
                        }
                        else if (cbTo.Text == "Percent")
                        {
                            val = (val * pxper);
                            rtbResult.Text = val.ToString();
                        }
                    }
                    else if (cbFrom.Text == "Points")
                    {
                        if (cbTo.Text == "Ems")
                        {
                            val = (val * ptsems);
                            rtbResult.Text = val.ToString();
                        }
                        else if (cbTo.Text == "Percent")
                        {
                            val = (val * ptsper);
                            rtbResult.Text = val.ToString();
                        }
                    }
                }
                catch
                {
                    rtbResult.Text = "Value not an integer";
                }
            }
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            btnGenerate.UseWaitCursor = true;

            double dfrom = 0;
            double dto = 0;
            double diteration = 0;
            bool bcheck = true;
            bool bvalid = true;
            
            try
            {
                dfrom = double.Parse(txtbxFrom.Text);
            }
            catch
            {
                MessageBox.Show("Please enter a valid amount for the starting value.");
                bcheck = false;
            }
            try
            {
                dto = double.Parse(txtbxTo.Text);
            }
            catch
            {
                MessageBox.Show("Please enter a valid amount for the ending value.");
                bcheck = false;
            }
            try
            {
                diteration = double.Parse(txtbxIteration.Text);
            }
            catch
            {
                MessageBox.Show("Please enter a valid amount for the iteration value.");
                bcheck = false;
            }

            if (bcheck)
            {
                if (txtbxFrom.Text == "")
                {
                    MessageBox.Show("Please enter a Starting value");
                    bvalid = false;
                }
                else if (txtbxTo.Text == "")
                {
                    MessageBox.Show("Please enter an Ending value");
                    bvalid = false;
                }
                else if (txtbxIteration.Text == "")
                {
                    MessageBox.Show("Please enter an iteration value");
                    bvalid = false;
                }
                else if (txtbxFrom.Text == txtbxTo.Text)
                {
                    MessageBox.Show("Starting value must be lower than ending value");
                    bvalid = false;
                }
                else if (dfrom >= dto)
                {
                    MessageBox.Show("Starting value must be lower than ending value");
                    bvalid = false;
                }
                else if (cbFrom.Text == "")
                {
                    MessageBox.Show("The Convert From drop down must have a value");
                    bvalid = false;
                }
                else if (cbTo.Text == "")
                {
                    MessageBox.Show("The Convert To drop down muc have a value.");
                    bvalid = false;
                }

                if (bvalid)
                {
                    saveFileDialog1.Filter = "html files (*.html)|*.html";
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {                        
                        try
                        {
                            OutputHTML(saveFileDialog1.FileName, dfrom, dto, diteration);
                        }
                        catch
                        {

                        }
                    }
                }
            }
            btnGenerate.UseWaitCursor = false;
        }

        private void OutputHTML(string sFilename, double dfrom, double dto, double diteration)
        {            
            TextWriter tw = new StreamWriter(sFilename);
            int ipres = 3;
            tw.WriteLine("<html><head><title>Calculated Values</title></head></html><body>");
            tw.WriteLine("<table border=\"1\" cellpadding=\"10\">");
            tw.WriteLine("<th>" + cbFrom.Text + "</th><th>" + cbTo.Text + "</th>");

            for (double i = dfrom; i <= dto; i += diteration)
            {                                
                tw.WriteLine("<tr><td>" + Math.Round(i,ipres) + "</td><td>" + Math.Round((i * double.Parse(txtbxFormula.Text)),ipres) + "</td></tr>");              
            }

            tw.WriteLine("</table>");
            tw.WriteLine("</body>");
            tw.WriteLine("</html>");

            tw.Close();

            if (key.GetValue("BrowserMsg").ToString() == "0")
            {                
                BrowserCheck bc = new BrowserCheck();
                if (bc.ShowDialog() == DialogResult.Yes)
                {
                    if (key.GetValue("BrowserMsg").ToString() == "1")
                    {
                        showBrowserMessageToolStripMenuItem.Checked = false;
                    }
                    else
                    {
                        showBrowserMessageToolStripMenuItem.Checked = true;
                    }

                    try
                    {
                        System.Diagnostics.Process.Start(sFilename);
                    }
                    catch
                    {
                        
                    }
                }
            }
            else
            {
                
            }
        }

        private void txtbxFrom_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtbxTo_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtbxIteration_TextChanged(object sender, EventArgs e)
        {

        }

        private void showBrowserMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(showBrowserMessageToolStripMenuItem.Checked == true)
            {
                key.SetValue("BrowserMsg", "1");
                showBrowserMessageToolStripMenuItem.Checked = false;
            }
            else
            {
                key.SetValue("BrowserMsg", "0");
                showBrowserMessageToolStripMenuItem.Checked = true;
            }
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }
    }
}